package com.xyhuo.leetcode.december;

/**
 * 跳跃游戏
 * https://leetcode.cn/problems/jump-game/
 *
 * @time 2023/12/22
 */
public class CanJump {

//    public static void main(String[] args) {
//        CanJump cj = new CanJump();
//        int[] nums = {2, 3, 1, 1, 4};
//        cj.canJump(nums);
//    }

    public boolean canJump(int[] nums) {
        int[] dp = new int[nums.length];
        dp[0] = 1;
        for (int i = 0; i < nums.length - 1; i++) {
            if (dp[i] == 1) {
                for (int j = 1; j <= nums[i]; j++) {
                    if (i + j >= nums.length - 1) {
                        return true;
                    }
                    dp[i + j] = 1;
                }
            }
        }
        return dp[nums.length - 1] == 1;
    }
}
